<?php

namespace App\Kuafu\Order\Command;

use App\Kuafu\Base\Command\Traits\KfGetTimeInterval;
use App\Kuafu\Order\Model\OrderCouponSplitModel;
use App\Kuafu\Order\Model\OrderDetailSplitModel;
use App\Kuafu\Order\Model\OrderDiscountSplitModel;
use App\Kuafu\Order\Model\OrderSplitModel;
use App\Library\FeiShu\SendFs;
use Illuminate\Console\Command;

class OrderStreetSweeperDeleted extends Command
{
    use KfGetTimeInterval;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:street-sweeper-deleted';

    /**
     * The console command description.
     *
     * @var string
     */

    protected $description = '夸父堂食订单清道夫 - 删除软删除的数据';


    /**
     * Execute the console command
     *
     * @return int
     */
    public function handle()
    {
        // 删除 半年 以前的 已删除的订单数据
        $delAfterDays = 30 * 86400; //  删除 180 天以前的 历史数据

        // 删除数据
        $queryTime = time() - $delAfterDays;
        for ($i = 0; $i <= 99; $i++) {

            // 主表数据
            OrderSplitModel::suffix($i)
                           ->query()
                           ->withoutGlobalScope('isDel')
                           ->where('isDeleted', 1)
                           ->where('createdAt', '<', $queryTime)
                           ->where('createdAt', '>', $queryTime - 86400 * 2)
                           ->delete();

            // 订单详情
            OrderDetailSplitModel::suffix($i)
                                 ->query()
                                 ->withoutGlobalScope('isDel')
                                 ->where('isDeleted', 1)
                                 ->where('createdAt', '<', $queryTime)
                                 ->where('createdAt', '>', $queryTime - 86400 * 2)
                                 ->delete();
            // 优惠券信息
            OrderCouponSplitModel::suffix($i)
                                 ->query()
                                 ->withoutGlobalScope('isDel')
                                 ->where('isDeleted', 1)
                                 ->where('createdAt', '<', $queryTime)
                                 ->where('createdAt', '>', $queryTime - 86400 * 2)
                                 ->delete();

            // 整单优惠
            OrderDiscountSplitModel::suffix($i)
                                   ->query()
                                   ->withoutGlobalScope('isDel')
                                   ->where('isDeleted', 1)
                                   ->where('createdAt', '<', $queryTime)
                                   ->where('createdAt', '>', $queryTime - 86400 * 2)
                                   ->delete();
        }

        SendFs::newRobot(SendFs::ROBOT_BUSINESS_WARING)
              ->setTitleContent('[' . date('Y-m-d H:i:s') . '] : ' . '堂食清道夫', '历史软删数据已删除')->send();

        return 0;
    }


}
